package com.caishi.lkx.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;

import com.caishi.lkx.common.CommonResultCode;
import com.caishi.lkx.user.mapper.SysDictDetailMapper;
import com.caishi.lkx.user.model.SysDictDetailModel;
import com.caishi.lkx.user.service.ISysDictDetailService;
import com.zzw.common.Wrappers;
import com.zzw.common.cache.CacheConstants;
import com.zzw.common.entity.IBaseMapper;
import com.zzw.common.exception.BizRuntimeException;
import com.zzw.common.service.NameService;
import com.zzw.common.service.service.SortService;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;


/**
 * @Param:
 * @return
 * @Author: huangpeng
 * @Date: 2021/9/23 9:37
 * @Description:
 */
@Service
public class SysDictDetailServiceImpl implements ISysDictDetailService, NameService<SysDictDetailModel,String>, SortService<SysDictDetailModel> {

    @Resource
    private SysDictDetailMapper sysDictDetailMapper;

    @Override
    public IBaseMapper<SysDictDetailModel, String> getMapper() {
        return sysDictDetailMapper;
    }

    @Override
    @CacheEvict(value = CacheConstants.BIG_LOW_LOW, key = "'sys:dict:detail:' + #entity.typeCode ",allEntries = true)
    public SysDictDetailModel insert(SysDictDetailModel entity) {
        SysDictDetailModel model = this.selectOne(Wrappers.<SysDictDetailModel>lambdaQuery()
                .select(SysDictDetailModel::getId)
                .eq(SysDictDetailModel::getCode, entity.getCode()));
        if (null != model) {
            throw new BizRuntimeException("编码已存在");
        }
        getMapper().insert(entity);
        return entity;
    }

    @Override
    @CacheEvict(value = CacheConstants.BIG_LOW_LOW, key = "'sys:dict:detail:'  ",allEntries = true)
    public Boolean delete(String id) {
        if (!this.canDelete(Collections.singletonList(id))) {
            throw new BizRuntimeException(CommonResultCode.dataNotAllowDelete);
        }
        return getMapper().deleteById(id) == 1;
    }

    @Override
    @CacheEvict(value = CacheConstants.BIG_LOW_LOW, key = "'sys:dict:detail:' ",allEntries = true)
    public Boolean delete(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            throw new BizRuntimeException(CommonResultCode.illegalArgument);
        }
//        if (ids.size() == 1) {
//            return this.delete(ids.get(0));
//        }
//        ids = ids.stream().distinct().collect(Collectors.toList());
        if (!this.canDelete(ids)) {
            throw new BizRuntimeException(CommonResultCode.dataNotAllowDelete);
        }
        if (getMapper().deleteBatchIds(ids) != ids.size()) {
            throw new BizRuntimeException(CommonResultCode.dataChangeError);
        }
        return true;
    }

    @Override
    @CacheEvict(value = CacheConstants.BIG_LOW_LOW, key = "'sys:dict:detail:' + #entity.typeCode ",allEntries = true)
    public SysDictDetailModel simpleUpdate(SysDictDetailModel entity) {
        if (ObjectUtil.isEmpty(entity.getId())) {
            throw new BizRuntimeException("update必须拥有Id", CommonResultCode.dataChangeError.getCode());
        }
        SysDictDetailModel model = this.selectOne(Wrappers.<SysDictDetailModel>lambdaQuery()
                .select(SysDictDetailModel::getId)
                .eq(SysDictDetailModel::getCode, entity.getCode()));
        if (null != model && !entity.getId().equals(model.getId())) {
            throw new BizRuntimeException("编码已存在");
        }
        return getMapper().updateById(entity) == 1 ? entity : null;
    }

    @Override
    @CacheEvict(value = CacheConstants.BIG_LOW_LOW, key = "'sys:dict:detail:' + #typeCode ",allEntries = true)
    public Boolean deleteByTypeCode(String typeCode) {
         this.delete(Wrappers.<SysDictDetailModel>lambdaUpdate().eq(SysDictDetailModel::getTypeCode, typeCode));
        return true;
    }

    @Override
    public List<SysDictDetailModel> selectByTypeCode(String typeCode) {
        return this.selectList(Wrappers.<SysDictDetailModel>lambdaQuery().eq(SysDictDetailModel::getTypeCode, typeCode).orderByAsc(SysDictDetailModel::getSort));
    }

    @Override
    public SysDictDetailModel selectByTypeAndCode(String typeCode, String code) {
      return   this.selectOne(Wrappers.<SysDictDetailModel>lambdaQuery()
                .eq(SysDictDetailModel::getTypeCode,typeCode)
                .eq(SysDictDetailModel::getCode, code));
    }

    @Override
    public SysDictDetailModel getByCode(String code) {
        return   this.selectOne(Wrappers.<SysDictDetailModel>lambdaQuery()
                .eq(SysDictDetailModel::getCode, code));
    }
}
